//
//  ViewController.m
//  14_UICollectionView
//
//  Created by mafeng on 16/8/2.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#define kCollectionView @"reuse"
#define kCollectionViewReusable @"reusable"
#define kCollectionViewReusableFooter @"reusablefoot"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 1. UICollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 1. itemSize 默认大小50 50
    // 2. scrollDirection 滚动方向
    //      UICollectionViewScrollDirectionHorizontal  水平
    //      UICollectionViewScrollDirectionVertical  竖直
    // UICollectionView创建
    // 1. 参数1: frame
    // 2. 参数2: 填入不能为空的layout(布局)
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    // 滚动方向改变, 会交换列和行(包括区)
    //layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 3. minimumLineSpacing 最小行间距
    layout.minimumLineSpacing = 5;
    // 4. minimumInteritemSpacing 最小列间距
    layout.minimumInteritemSpacing = 5;
    // 5. sectionInset
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    UICollectionView *mycollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [self.view addSubview:mycollectionView];
    // 6. headerReferenceSize 区头大小 横向滚动时, 设置宽; 纵向滚动时, 设置高;
    layout.headerReferenceSize = CGSizeMake(100, 100);
    // 7. footerReferenceSize 区尾大小 横向滚动时, 设置宽; 纵向滚动时, 设置高;
    layout.footerReferenceSize = CGSizeMake(100, 100);
    mycollectionView.delegate = self;
    mycollectionView.dataSource = self;
    // 8. 区头区尾的悬浮效果
    layout.sectionHeadersPinToVisibleBounds = YES;
    layout.sectionFootersPinToVisibleBounds = YES;
    [mycollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionView];
    // 注册区头
    [mycollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusable];
    // 注册区尾
    [mycollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewReusableFooter];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
// 区头和区尾的协议方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusable forIndexPath:indexPath];
        view.backgroundColor = [UIColor blueColor];
        return view;
    } else {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewReusableFooter forIndexPath:indexPath];
        view.backgroundColor = [UIColor redColor];
        return view;
    }
}
// 返回cell的样式
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor greenColor];
    return cell;
}
 // 返回不同setion的cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(30, 30);
    }
    return CGSizeMake(120, 120);  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
